/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-07-05     lyh       the first version
 */

#include "l4_aht10.h"

/*定义aht10_dervice 设备句柄*/
static aht10_device_t aht10_device;
/*定义aht10_线程句柄*/
static rt_thread_t aht10_thread;
static rt_thread_t wlan_thread;

//定义温湿度数据变量
static float tmpe;
static float humi;

/*创建线程入口函数*/
static void aht10_thread_entry(void* parameter)
{
    aht10_device = aht10_init(AHT10_I2C_BUS);
    char *cjson_str = NULL;
    cJSON *jsonDataRoot = cJSON_CreateObject();
    do
    {
        tmpe = aht10_read_temperature(aht10_device);
        humi = aht10_read_humidity(aht10_device);
        cJSON_AddItemToObject(jsonDataRoot, "tmpe", cJSON_CreateNumber(tmpe));
        cJSON_AddItemToObject(jsonDataRoot, "humi", cJSON_CreateNumber(humi));
        cjson_str = cJSON_Print(jsonDataRoot);
        mymqtt_pub(cjson_str);
        rt_kprintf("****************\n");
        rt_kprintf("tmpe is %d.%d \r\n", (int) (tmpe * 10) / 10, (int) (tmpe * 10) % 10);
        rt_kprintf("humi is %d.%d \r\n", (int) (humi * 10) / 10, (int) (humi * 10) % 10);
        rt_kprintf("****************\n");
        rt_thread_mdelay(3000);
        cJSON_DeleteItemFromObject(jsonDataRoot, "tmpe");
        cJSON_DeleteItemFromObject(jsonDataRoot, "humi");
    } while (1);
}

void aht10_thread_create(void)
{
    mqtt_main();
    rt_thread_mdelay(300);
    aht10_thread = rt_thread_create("aht10_thread", aht10_thread_entry,
    RT_NULL, 2048,
    RT_THREAD_PRIORITY_MAX - 2, 10);

    if (aht10_thread != NULL)
    {
        rt_thread_startup(aht10_thread);
    }
}
void aht10_readData(void)
{
    aht10_thread_create();
}
MSH_CMD_EXPORT(aht10_readData, aht10_readData);
